package com.yidu.hbats.action.TrainingPlan;

import com.yidu.hbats.action.vo.AtsPlanYearMorevo;
import com.yidu.hbats.dao.AtsEmpAthleteSchemeMapper;
import com.yidu.hbats.model.*;
import com.yidu.hbats.service.TrainingPlan.AtsEmpAthleteSchemeService;
import com.yidu.hbats.service.emp.AtsEmpAthleteService;
import com.yidu.hbats.service.atsBase.AtsBaseSportService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanItemService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanYearMoreService;
import com.yidu.hbats.service.emp.AtsEmpCoachService;
import com.yidu.utils.AtsPlanItemTree;
import com.yidu.utils.FileTools;
import com.yidu.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @description  多年年度计划的action类
 * @author: 韩红
 * @create: 2019-10-30 11:12
 **/
@Controller
@RequestMapping("/atsPlanYearMoreController")
public class AtsPlanYearMoreController {
    Logger logger= (Logger) Logger.getLogger(String.valueOf(AtsPlanYearMoreController.class));
    @Resource
    AtsPlanYearMoreService atsPlanYearMoreService;
    //教练表的注入
    @Resource
    AtsEmpCoachService atsEmpCoachService;
    //体育训练表的注入
    @Resource
    AtsBaseSportService atsBaseSportService;
    //训练计划指标
    @Resource
    AtsPlanItemService atsPlanItemService;
    //运动员接口
    @Resource
    AtsEmpAthleteService atsEmpAthleteService;
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;
    //文件上传管理工具
    @Resource
    FileTools fileTools;

    /**
     * 逻辑删除
     * @param atsPlanYearMore
     * @return
     */
    @RequestMapping("updateIsva")
    @ResponseBody
    public Message updateIsva(AtsPlanYearMore atsPlanYearMore) {
        //逻辑删除多年度计划
        int rows = atsPlanYearMoreService.updateIsva(atsPlanYearMore);
        //删除计划同时删除下面指标
        AtsPlanItem atsPlanItem=new AtsPlanItem();
        atsPlanItem.setIsva(0);
        atsPlanItem.setPid(atsPlanYearMore.getPid());
        //删除对应的运动员
        AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
        atsEmpAthleteScheme.setIsva(0);
        atsEmpAthleteScheme.setPid(atsPlanYearMore.getPid());
        Message mes = new Message();
        if (rows >= 1) {
            int rowso=atsEmpAthleteSchemeService.updatePid(atsEmpAthleteScheme);
            int rowst=atsPlanItemService.updateOrdelete(atsPlanItem);
            mes.setNum(1);
            mes.setMsg("修改成功");
        } else {
            mes.setNum(0);
            mes.setMsg("修改失败");
        }
        return mes;
    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @RequestMapping("delAll")
    @ResponseBody
    public Message delAll(String id) {
        int rows = atsPlanYearMoreService.delAll(id);
        Message mes = new Message();
        if (rows >= 1) {
            mes.setNum(1);
            mes.setMsg("修改成功");
        } else {
            mes.setNum(0);
            mes.setMsg("修改失败");
        }


        return mes;
    }

    /**
     * 根据ID查询多年度计划
     * @param atsPlanItem
     * @param atsPlanYearMore
     * @return
     */
    @RequestMapping("moreById")
    @ResponseBody
    public Map<String, Object> findById(AtsPlanItem atsPlanItem, AtsPlanYearMore atsPlanYearMore) {
        Map<String, Object> map = atsPlanItemService.yaerMoreSelectPid(atsPlanItem, atsPlanYearMore);
        return map;
    }

    /**
     * 多年度计划展示
     * @param atsPlanItem
     * @return
     */
    @RequestMapping("yearMoreEchar")
    @ResponseBody
    public List<AtsPlanItemTree> echar(AtsPlanItem atsPlanItem) {
        List<AtsPlanItemTree>  treeList = atsPlanItemService.yearMoreEchar(atsPlanItem);
        return treeList;

    }

    /**
     * 查询所有
     * @param request
     * @param atsPlanYearMore
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String, Object> selectAll(HttpServletRequest request, AtsPlanYearMore atsPlanYearMore, @RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize) {
        int count = atsPlanYearMoreService.count(request,atsPlanYearMore);
        List<AtsPlanYearMore> list = atsPlanYearMoreService.selectAll(request,atsPlanYearMore,pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        map.put("count", count);
        map.put("code", 0);
        map.put("msg", "");//消息
        return map;
    }


    /**
     * 显示所有数据
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAllandPage")
    public Map<String,Object> selectAllandPage(){
        List<AtsPlanYearMore> selectAllandPage=atsPlanYearMoreService.selectAllandPage();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count",1);
        map.put("data", selectAllandPage);
        return map;
    }

    /**
     * 教练员下拉框赋值方法
     * @return
     */
    @ResponseBody
    @RequestMapping("findxiaAll")
    public List<AtsEmpCoach> findxiaAll(){
        List<AtsEmpCoach> list=atsEmpCoachService.findxiaAll();
        return list;
    }
    /**
     * 运动计划下拉框赋值方法
     * @return
     */
    @ResponseBody
    @RequestMapping("findtyAll")
    public List<AtsBaseSport> findtyAll(){
        List<AtsBaseSport> list=atsBaseSportService.findAll();
        return list;
    }

    /**
     * 年度计划增加或修改方法
     * @param atsPlanYearMorevo 返回vo数据
     * @param session 得到操作人
     * @return
     */
    @ResponseBody
    @RequestMapping("addOrupdate")
    public Message addOrupdate(AtsPlanYearMorevo atsPlanYearMorevo, HttpSession session){
        atsPlanYearMorevo.setRemark("5");
        atsPlanYearMorevo.setIsva(1);
        Message message=new Message();
        //根据下拉框的教练id查出教练名
        AtsEmpCoach atsEmpCoach=atsEmpCoachService.findById(atsPlanYearMorevo.getCoachId());
        //传入体育项目名
        atsPlanYearMorevo.setItemName(atsPlanYearMorevo.getSportName());
        //传入教练名
        atsPlanYearMorevo.setCoachName(atsEmpCoach.getCoachName());
        //判断附件是否为空
        if(atsPlanYearMorevo.getFileone().getOriginalFilename()!=null &&!"".equals(atsPlanYearMorevo.getFileone().getOriginalFilename())){
            try {
                String file1=fileTools.saveFile("TrainingPlan",atsPlanYearMorevo.getFileone());
                atsPlanYearMorevo.setFile1(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(atsPlanYearMorevo.getFiletwo().getOriginalFilename()!=null &&!"".equals(atsPlanYearMorevo.getFiletwo().getOriginalFilename())){
            try {
                String file2 = fileTools.saveFile("TrainingPlan",atsPlanYearMorevo.getFiletwo());
                atsPlanYearMorevo.setFile2(file2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(atsPlanYearMorevo.getFilethree().getOriginalFilename()!=null &&!"".equals(atsPlanYearMorevo.getFilethree().getOriginalFilename())){
            try {
                String file3=fileTools.saveFile("TrainingPlan",atsPlanYearMorevo.getFilethree());
                atsPlanYearMorevo.setFile3(file3);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //ManagerModel managerModel= (ManagerModel) session.getAttribute("manager");
        //取到当前登录人
        //managerModel.getManUser();
        //atsPlanYearMorevo.setCreater("admin");
        //给指标传创建人
        //atsPlanYearMorevo.setCreateName("");
        if(atsPlanYearMorevo.getPid()!=null &&!"".equals(atsPlanYearMorevo.getPid())){
            atsPlanYearMoreService.updateByPrimaryKeySelective(atsPlanYearMorevo);
        }else{
            atsPlanYearMoreService.insertSelective(atsPlanYearMorevo);
        }
        //传成功
        message.setNum(1);
        message.setMsg("操作成功!");
        return message;
    }

    /**
     * 根据主键赋值
     * @param pid
     * @return
     */
    @ResponseBody
    @RequestMapping("findById")
    public AtsPlanYearMore findById(Integer pid){
        AtsPlanYearMore atsPlanYearMore=atsPlanYearMoreService.selectByPrimaryKey(pid);
        return atsPlanYearMore;
    }

    /**
     * 记忆功能，查最大的id
     * @return
     */
    @ResponseBody
    @RequestMapping("findMaxPid")
    public AtsPlanYearMore findMaxPid(int coachId){
        AtsPlanYearMore atsPlanYearMore=atsPlanYearMoreService.findMaxPid(coachId);
        return atsPlanYearMore;
    }

    /**
     * 查询多年度计划
     * @param pid
     * @return
     */
    @ResponseBody
    @RequestMapping("findPlanItemYearMore")
    public List<AtsPlanItem> findPlanItemYearMore(Integer pid){
        List<AtsPlanItem> list=atsPlanItemService.findPlanItemYearMore(pid);
        return list;
    }

    /**
     * 查询多年度计划数据图表
     * @param pid
     * @return
     */
    @ResponseBody
    @RequestMapping("findPlanItemYearMoreone")
    public List<AtsPlanItem> findPlanItemYearMoreone(Integer pid){
        List<AtsPlanItem> list=atsPlanItemService.findPlanItemYearMoreone(pid);
        return list;
    }

    /**
     * 删除指标
     * @param itemId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteZhibiao")
    public Message deleteZhibiao(Integer itemId){
        AtsPlanItem atsPlanItem=new AtsPlanItem();
        atsPlanItem.setIsva(0);
        atsPlanItem.setItemId(itemId);
        Message message=new Message();
        int rows=atsPlanItemService.updateByPrimaryKeySelective(atsPlanItem);
        if(rows>0){
            message.setMsg("删除成功!");
            message.setNum(1);
        }else{
            message.setMsg("删除失败!");
            message.setNum(0);
        }
        return message;
    }

    /**
     * 查询数据
     * @return
     */
    @ResponseBody
    @RequestMapping("findDivone")
    public List<AtsPlanItem> findDivone(){
        List<AtsPlanItem> list=atsPlanItemService.findDivone();
        return list;
    }

    /**
     * 查询运动员
     * @param coachId
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAthlete")
    public List<AtsEmpAthlete> selectAthlete(int coachId){
        List<AtsEmpAthlete> list=atsEmpAthleteService.selectByCoachIdone(coachId);
        return list;
    }
    @ResponseBody
    @RequestMapping("findCoachexp")
    public AtsEmpCoach findCoachexp(int coachId){
        AtsEmpCoach atsEmpCoach = atsEmpCoachService.findById(coachId);
        return atsEmpCoach;
    }
    @ResponseBody
    @RequestMapping("findCoachexpone")
    public AtsBaseSport selectByPrimaryKey(int sportId){
        AtsBaseSport atsBaseSport=atsBaseSportService.selectByPrimaryKey(sportId);
        return atsBaseSport;
    }
    @ResponseBody
    @RequestMapping("findByIdpid")
    public List<AtsEmpAthleteScheme> findByIdpid(int pid){
        List<AtsEmpAthleteScheme> list= atsEmpAthleteSchemeService.findByPid(pid);
        return list;
    }
    @ResponseBody
    @RequestMapping("findByIdpidTwo")
    public List<AtsEmpAthleteScheme> findByIdpidTwo(int pid){
        List<AtsEmpAthleteScheme> list= atsEmpAthleteSchemeService.findByPidTwo(pid);
        return list;
    }
    @ResponseBody
    @RequestMapping("findByIdpidThree")
    public List<AtsEmpAthleteScheme> findByIdpidThree(int pid){
        List<AtsEmpAthleteScheme> list= atsEmpAthleteSchemeService.findByPidThree(pid);
        return list;
    }
    @ResponseBody
    @RequestMapping("findByIdpidFour")
    public List<AtsEmpAthleteScheme> findByIdpidFour(int pid){
        List<AtsEmpAthleteScheme> list= atsEmpAthleteSchemeService.findByPidFour(pid);
        return list;
    }
    @ResponseBody
    @RequestMapping("findByIdpidFive")
    public List<AtsEmpAthleteScheme> findByIdpidFive(int pid){
        List<AtsEmpAthleteScheme> list= atsEmpAthleteSchemeService.findByPidFive(pid);
        return list;
    }
}
